import { Body, Controller, DefaultValuePipe, Delete, Get, Param, Post, Put, Query } from '@nestjs/common'
import { generateParseIntPipe } from 'src/utils'
import { RequireLogin } from 'src/custom/decorator'
import { MenuService } from './menu.service'
import { CreateMenuDto } from './dto/create-menu.dto'
import { UpdateMenuDto } from './dto/update-menu.dto'

@Controller('menu')
export class MenuController {
  constructor(private readonly menuService: MenuService) {}

  @Get('list')
  async list(
      @Query('pageNo', new DefaultValuePipe(1), generateParseIntPipe('pageNo')) pageNo: number,
      @Query('pageSize', new DefaultValuePipe(2), generateParseIntPipe('pageSize')) pageSize: number,
      @Query('name') name: string,
  ) {
    return await this.menuService.find(pageNo, pageSize, name)
  }

  @Post('create')
  async create(@Body() menuDto: CreateMenuDto) {
    return await this.menuService.create(menuDto)
  }

  @Put('update')
  async update(@Body() menuDto: UpdateMenuDto) {
    return await this.menuService.update(menuDto)
  }

  @Delete('delete')
  @RequireLogin()
  async delete(@Body('id') id: number) {
    return await this.menuService.delete(id)
  }
}
